//
//  PrivateMessageListVC.m
//  jingdongfang
//
//  Created by mac on 16/9/3.
//  Copyright © 2016年 ZhiYu Hao. All rights reserved.
//

#import "PrivateMessageListVC.h"
#import "PrivateMessageListFooterView.h"
#import "TLChatViewController.h"
#import "PrivateMTableViewCell.h"
#import "IGImMsgListApi.h"
#import "ImMsgModel.h"
#import "IGHandleMsgApi.h"

@interface PrivateMessageListVC ()

@property (nonatomic, weak) IBOutlet UITableView *tableView;
@property (nonatomic, strong) PrivateMessageListFooterView * footerView;
@property (nonatomic, strong) IGImMsgListApi *imListApi;
@property (nonatomic, strong) IGHandleMsgApi *handlMsgApi;
@property (nonatomic, strong) UIButton *selectedBtn;//选择按钮
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) NSMutableArray *chooseArr;//选中数据的数组
@property (nonatomic, strong) NSMutableArray *markArr;//标记数据的数组
@property (nonatomic, strong) NSMutableArray *msgArray;
@property (nonatomic, assign) NSInteger pageNumber;
@property (nonatomic, copy) NSString *lastTimeDate;

@end

@implementation PrivateMessageListVC
#pragma mark -懒加载
-(NSMutableArray *)msgArray{
    if (!_msgArray) {
        _msgArray=[NSMutableArray array];
    }
    return _msgArray;
}
#pragma mark -生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
//     self.title = @"私信";
    self.title = NSLocalizedString(@"Private Message", nil);
    self.chooseArr = [NSMutableArray array];
    self.markArr = [NSMutableArray array];
    //选择按钮
    _selectedBtn = [UIButton buttonWithType:UIButtonTypeCustom];

    [_tableView registerClass:[PrivateMTableViewCell class] forCellReuseIdentifier:@"PrivateMTableViewCell"];
    _tableView.tableFooterView = [UIView new];
    _pageNumber = 1;
    _lastTimeDate = @"";
    self.imListApi = [IGImMsgListApi new];
    [self.tableView ig_setHeaderWithRefreshingBlock:^{
        _pageNumber = 1;
        _lastTimeDate = @"";
        [self requestData];
    }];
}
-(void)requestData{
    _imListApi.pageNum=_pageNumber;
    _imListApi.lastTimeDate=_lastTimeDate;
    @KWeakObj(self)
    [self.imListApi startWithDecodeClass:[IGImMsgListResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
        [selfWeak.tableView ig_endHeaderRefreshing];
        [selfWeak.tableView ig_endFooterRefreshing];
        if (responseObject) {
            IGImMsgListResp *resp=responseObject;
            IGImMsgListModel *model=resp.data;
            if (model.msgs&&model.msgs.count>0) {
                [selfWeak setupFooter];
            } else {
                [_tableView ig_endFooterRefreshingWithNoMoreData];
            }
            if (_pageNumber == 1)  {
                _msgArray = [NSMutableArray arrayWithArray:model.msgs];
            } else {
                [_msgArray addObjectsFromArray:model.msgs];
            }
            [self.tableView reloadData];
        }
        else {
            //空白页处理逻辑写在这里
        }
    }];
}

- (void)setupFooter
{
    @KWeakObj(self)
    [self.tableView ig_setFooterWithRefreshingBlock:^{
        _pageNumber++;
        [selfWeak requestData];
    }];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.tableView ig_beginHeaderRefreshing];
}

#pragma mark - UITableViewDelegate UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _msgArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    PrivateMTableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:@"PrivateMTableViewCell"];
    ImMsgModel * siXinModel = _msgArray[indexPath.row];
    cell.siXinModel = siXinModel;
    [cell setNeedsLayout];
    return cell;

}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return  60;
}
//选中时将选中行的在self.dataArray 中的数据添加到删除数组self.deleteArr中
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (self.tableView.editing) {
        [self.chooseArr addObject:[self.dataArray objectAtIndex:indexPath.row]];
    }else{
        ImMsgModel * siXinModel = _msgArray[indexPath.row];
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
        TLChatViewController * vc  = [[TLChatViewController alloc]init];
        vc.otherId=siXinModel.senderId;
        vc.navigationItem.title = siXinModel.senderNick;
        [UIViewController ig_pushViewController:vc animated:YES];
    }
    
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    ImMsgModel * sixinMessageModel = _msgArray[indexPath.row];
    self.handlMsgApi = [IGHandleMsgApi new];
    self.handlMsgApi.type = @"5";
    self.handlMsgApi.msgId = sixinMessageModel.senderId;
    [self.handlMsgApi startWithDecodeClass:[IGHandleMsgResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
        if (responseObject) {
            // 删除模型
            [_msgArray removeObjectAtIndex:indexPath.row];
            // 刷新
            [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
        }
    }];
}

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
//    return @"删除";
    return NSLocalizedString(@"Delete", nil);
}

//取消选中时 将存放在self.deleteArr中的数据移除
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath  {
    
    [self.chooseArr removeObject:[self.msgArray objectAtIndex:indexPath.row]];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10.0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.001f;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
